﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Configuration;
using System.IO;
using DataAccess;

namespace Spider
{
    namespace Dygod
    {
        /// <summary>
        /// 综艺页面
        /// </summary>
        public class ShowHome : Spider
        {
            /// <summary>
            /// 构造函数
            /// </summary>
            public ShowHome()
            {
                HomeUrl = new Uri("https://www.dygod.net/");
                CurrentUrl = new Uri("https://www.dygod.net/html/gndy/");
            }

            /// <summary>
            /// 开始抓取工作
            /// <returns>是否成功</returns>
            /// </summary>
            public override bool DoWork()
            {
                Public.WriteLog("----- Dygod ShowHome DoWork -----");
                try
                {
                    // 下载页面源代码
                    Public.WriteLog(CurrentUrl.ToString());
                    string content = Encoding.UTF8.GetString(new WebClient().DownloadData(CurrentUrl));
                    Public.WriteLog("Dygod ShowHome DownloadData success!");
                    // 根据正则表达式进行过滤
                    List<string> urls = new List<string>();
                    {
                        Regex regex = new Regex("html/gndy/dyzz/\\d+/\\d+.html");
                        foreach (Match m in regex.Matches(content))
                        {
                            urls.Add(HomeUrl.ToString() + m.Value);
                        }
                    }
                    {
                        Regex regex = new Regex("html/gndy/jddy/\\d+/\\d+.html");
                        foreach (Match m in regex.Matches(content))
                        {
                            urls.Add(HomeUrl.ToString() + m.Value);
                        }
                    }
                    // 去除重复元素
                    urls = urls.Distinct().ToList<string>();
                    foreach (string iter in urls)
                    {
                        ShowDetail spider = new ShowDetail();
                        spider.HomeUrl = HomeUrl;
                        spider.CurrentUrl = new Uri(iter);
                        spider.DoWork();
                    }

                }
                catch (WebException e)
                {
                    Public.WriteLog("Dygod ShowHome WebException: " + e.ToString());
                }
                catch
                {
                    Public.WriteLog("Dygod ShowHome Exception: unknown！");
                }
                return true;
            }
        }
    }
}